<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <!-- <div class="avater_box">
        <img src="../assets/logo.png" alt="" />
      </div> -->
      <div style="margin-left: 200px;margin-top:20px">
        <p class="serif">登录</p>
      </div>
      <!-- 登陆表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="name">
          <el-input
            v-model="loginForm.name"
            style="width:400px"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="pwd">
          <el-input
            v-model="loginForm.pwd"
            style="width:400px"
            prefix-icon="el-icon-s-goods"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns" prop="kind">
          <el-radio v-model="loginForm.kind" label="0">商家</el-radio>
          <el-radio v-model="loginForm.kind" label="1">顾客</el-radio>
          <el-radio v-model="loginForm.kind" label="2">管理员</el-radio>
        </el-form-item>
        <el-form-item class="btns2">
          <el-button type="primary" @click="loginIn">登录</el-button>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog
      title="用户注册"
      :visible.sync="this.registerVisibale"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form label-position="left"
      :model="registerForm"
      :rules="registerFormRules"
      ref="editFormRef"
      label-width="90px"
      >
      <el-form-item label="用户名" prop="name" >
        <el-input
          v-model.number="registerForm.name"
          placeholder="请输入用户名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model.number="registerForm.phone"
          placeholder="请输入联系电话"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="类别" prop="kind">
        <el-radio v-model="registerForm.kind" label="0">商家</el-radio>
        <el-radio v-model="registerForm.kind" label="1">顾客</el-radio>
        <el-radio v-model="registerForm.kind" label="2">管理员</el-radio>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="registerForm.sex" label="0">男</el-radio>
        <el-radio v-model="registerForm.sex" label="1">女</el-radio>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input
          v-model.number="registerForm.pwd"
          placeholder="请输入密码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="addr">
        <el-input
          v-model.number="registerForm.addr"
          placeholder="请输入地址"
          class="inputAddr"
          clearable
        ></el-input>
        <el-button @click="getLocation" style="display:inline-block">获取定位</el-button>
      </el-form-item>
      <el-form-item>
        <el-button  style="float:right" type="primary" @click="registerIn">确认</el-button>
      </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'

import myBMap from './map.js'

export default {
  data() {
    return {
      // 登录表单的数据
      // 教师
      registerVisibale:false,
      loginForm: {
        name : '',
        pwd:'',
        kind:''
      },
      registerForm:{
        name:'',
        phone:'',
        sex: '',
        pwd:'',
        addr:'',
        kind:''
      },
      // 表单验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
      registerFormRules:{
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
        phone:[
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            required: true,
            pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur',
          },


        ],

      }
    }
  },
  methods: {
    //获取定位
  getLocation(){
    myBMap.init().then(BMap => {
        new BMap.Geolocation().getCurrentPosition(
          position => {
            // if (position.accuracy) {
            //  	//用户允许的操作，position里面有城市，省份，经纬度等信息
            // } else {
            // 	//用户点击拒绝的操作
            //   // Dialog.confirm({
            //   //   title: '温馨提示!',
            //   //   message: '请打开手机定位功能',
            //   // }).then(() => {
            //   //  })
            //    this.$message.error('请打开定位功能')
            // }
              let address=position.address
             this.registerForm.addr=address.province + address.city + position.latitude +','+ position.longitude
            console.log(position)
          },
          error => {
            Dialog.alert({
              title: "提示",
              message: "获取定位失败",
            });
          } 
        
            );
          })
  },
    editDialogClosed() {
      // this.$refs.registerForm.resetFields()
      this.registerVisibale = false
    },
    // 点击重置按钮
    resetLoginForm() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields()
    },
    // 点击登录按钮
    loginIn() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return
        // const { data: res } = await this.$http.post('login/', this.loginForm)
        console.log(this.loginForm)
        axios.put('/initial/login',this.loginForm)
        .then((res=>{
          console.log(res)
        if (res.data.state != 0) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // 1.将登录成功之后的 token，保存到客户端的 sessionStorage
        window.sessionStorage.setItem('token', res.data.token)
        window.sessionStorage.setItem('user', res.data.name)
        if(this.loginForm.kind == '0') this.$router.push('/teacher/home')
        if(this.loginForm.kind == '1') this.$router.push('/student/home')
        if(this.loginForm.kind == '2') this.$router.push('/admin/home')
        // 2.通过编程式导航跳转到后台主页，路由地址
        // 跳转到管理员页面
        // if (res.data.user_type == 'A') this.$router.push('/admin/home')
        // // 跳转到学生页面
        // if (res.data.user_type == 'S') this.$router.push('/student/home')
        // // 跳转到教师页面
        // if (res.data.user_type == 'T') this.$router.push('/teacher/home')
        }))
        
      })
    },
    registerIn() {
      console.log(this.registerForm)
      axios.post('/initial/register',this.registerForm) 
      .then((res)=>{
        console.log(res)
        if (res.data.state !== 0) return this.$message.error('注册失败')
        this.$message.success('注册成功')
      })
      this.registerVisibale = false
    },
    register() {
      this.registerVisibale = true
      this.registerForm={
        name:'',
        phone:'',
        sex: '',
        pwd:'',
        addr:'',
        kind:''
      }
    }
  },
}
</script>

<style lang="less" scoped>
p.serif{
  font-family:"微软雅黑";
  margin-top: -20px;
  margin-bottom: -20px;
  margin-right: 10px;
  font-size:25px;
  display: inline;
}
.inputAddr {
  width:500px;
  margin-right: 20px;
}
.login_container {
  background-color: #2b4b6b;
  background-image: url('../assets/bg.jpg');
  background-size: 100% 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login_box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
}

.avater_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #eee;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  position: absolute;
  bottom: 0%;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  margin-left: 100px;
  justify-content: flex-end;
}
.btns2 {
  margin-left: 100px;
}
</style>